
public class IPUtil {


    public static void main(String[] args) {
        System.out.println(getIPAddress("255.255.255.255"));
    }

    public static int getIPAddress(String ipAddress) {
        if (ipAddress.length() < 7 || ipAddress.length() > 15) return -1;
        String[] split = ipAddress.split("\\.");
        if (split.length != 4 ) return -1;
        int[] ips = new int[4];
        for (int i = 0; i < split.length; i++) {
            int ip = 0;
            try {
                ip = Integer.parseInt(split[i]);
                if (ip == 0 && split[i].length() != 1) return -1;
                if (ip < 0 || ip > 255) return -2;
            } catch (Exception e) {
                return -1;
            }
            ips[i] = ip;
        }

        int[] arr = new int[32];
        int index = 0;
        for (int i = 3; i >= 0; i--) {
            int t = ips[i];
            for (int j = 0; j < 8; j++) {
                arr[index++] = t % 2;
                t /= 2;
            }
        }
        int res = 0;
        for (int i = 0; i < 32; i++) {
            if (arr[i] == 1) {
                res += (int) Math.pow(2, i);
            }
        }
        return res;
    }
}
